#include "Timer0.h"
#include "gd32f4xx.h"
#include "systick.h"

#if USE_TIMER0
void timer0_init() {
    uint32_t timer_rcu = RCU_TIMER0;             // 时钟信号
    uint32_t timer = TIMER0;                     //  哪个timer
    uint32_t timer_param_psc = TIMER0_PSC;       // 预分频系数
    uint32_t timer_param_period = TIMER0_PERIOD; // 设置频率
    /************************ timer 通用配置 ************************/
    // 初始化定时器相关配置
    rcu_periph_clock_enable(timer_rcu); // 初始化时钟信号
    timer_deinit(timer);                // 初始化timer
    timer_parameter_struct tps;
    timer_struct_para_init(&tps);    // 配置默认值
    tps.prescaler = timer_param_psc; // 根据实际需求去修改值
    tps.period = timer_param_period; // 周期计数（从0开始计数）
    timer_init(timer, &tps);
    /************************ timer 通用配置结束 ************************/
    /************************ GPIO 通用配置开始 ************************/
#if TIMER0_CH0_P_ENABLE
    // gpio初始化 ch0
    rcu_periph_clock_enable(TIMER0_CH0_P_PORT_RCU);
    gpio_mode_set(TIMER0_CH0_P_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, TIMER0_CH0_P_PIN);
    gpio_output_options_set(TIMER0_CH0_P_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, TIMER0_CH0_P_PIN);
    gpio_af_set(TIMER0_CH0_P_PORT, TIMER0_AF, TIMER0_CH0_P_PIN);
#endif

#if TIMER0_CH0_N_ENABLE
    // gpio初始化 ch0
    rcu_periph_clock_enable(TIMER0_CH0_N_PORT_RCU);
    gpio_mode_set(TIMER0_CH0_N_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, TIMER0_CH0_N_PIN);
    gpio_output_options_set(TIMER0_CH0_N_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, TIMER0_CH0_N_PIN);
    gpio_af_set(TIMER0_CH0_N_PORT, TIMER0_AF, TIMER0_CH0_N_PIN);
#endif

#if TIMER0_CH1_P_ENABLE
    // gpio初始化 CH1
    rcu_periph_clock_enable(TIMER0_CH1_P_PORT_RCU);
    gpio_mode_set(TIMER0_CH1_P_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, TIMER0_CH1_P_PIN);
    gpio_output_options_set(TIMER0_CH1_P_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, TIMER0_CH1_P_PIN);
    gpio_af_set(TIMER0_CH1_P_PORT, TIMER0_AF, TIMER0_CH1_P_PIN);
#endif

#if TIMER0_CH1_N_ENABLE
    // gpio初始化 CH1
    rcu_periph_clock_enable(TIMER0_CH1_N_PORT_RCU);
    gpio_mode_set(TIMER0_CH1_N_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, TIMER0_CH1_N_PIN);
    gpio_output_options_set(TIMER0_CH1_N_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, TIMER0_CH1_N_PIN);
    gpio_af_set(TIMER0_CH1_N_PORT, TIMER0_AF, TIMER0_CH1_N_PIN);
#endif
#if TIMER0_CH2_P_ENABLE
    // gpio初始化 CH2
    rcu_periph_clock_enable(TIMER0_CH2_P_PORT_RCU);
    gpio_mode_set(TIMER0_CH2_P_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, TIMER0_CH2_P_PIN);
    gpio_output_options_set(TIMER0_CH2_P_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, TIMER0_CH2_P_PIN);
    gpio_af_set(TIMER0_CH2_P_PORT, TIMER0_AF, TIMER0_CH2_P_PIN);
#endif

#if TIMER0_CH2_N_ENABLE
    // gpio初始化 CH2
    rcu_periph_clock_enable(TIMER0_CH2_N_PORT_RCU);
    gpio_mode_set(TIMER0_CH2_N_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, TIMER0_CH2_N_PIN);
    gpio_output_options_set(TIMER0_CH2_N_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, TIMER0_CH2_N_PIN);
    gpio_af_set(TIMER0_CH2_N_PORT, TIMER0_AF, TIMER0_CH2_N_PIN);
#endif
#if TIMER0_CH3_P_ENABLE
    // gpio初始化 CH3
    rcu_periph_clock_enable(TIMER0_CH3_P_PORT_RCU);
    gpio_mode_set(TIMER0_CH3_P_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, TIMER0_CH3_P_PIN);
    gpio_output_options_set(TIMER0_CH3_P_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, TIMER0_CH3_P_PIN);
    gpio_af_set(TIMER0_CH3_P_PORT, TIMER0_AF, TIMER0_CH3_P_PIN);
#endif
    /************************ GPIO 通用配置结束 ************************/
    /************************ 通道配置开始 *****************************/
    timer_oc_parameter_struct tops;
    // -- -- -- -- -- -- -- -- - CH0 -- -- -- -- -- -- -- -- -- -- --
#if TIMER0_CH0_ENABLE
    timer_channel_output_struct_para_init(&tops);
#if TIMER0_CH0_P_ENABLE
    tops.outputstate = TIMER_CCX_ENABLE;
    tops.ocpolarity = TIMER0_CH0_P_POLARITY;
#endif
#if TIMER0_CH0_N_ENABLE
    tops.outputnstate = TIMER_CCXN_ENABLE;
    tops.ocnpolarity = TIMER0_CH0_N_POLARITY;
#endif   
    // N和P
    tops.outputstate = (uint16_t)TIMER_CCX_ENABLE;
    timer_channel_output_config(timer, TIMER_CH_0, &tops);
    timer_channel_output_mode_config(timer, TIMER_CH_0, TIMER_OC_MODE_PWM0);
    timer_channel_output_pulse_value_config(timer, TIMER_CH_0, 0); // pwm值 初始duty =0
#endif
    //-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

    // -- -- -- -- -- -- -- -- - CH1 -- -- -- -- -- -- -- -- -- -- --
#if TIMER0_CH1_ENABLE
    timer_channel_output_struct_para_init(&tops);
#if TIMER0_CH1_P_ENABLE
    tops.outputstate = TIMER_CCX_ENABLE;
    tops.ocpolarity = TIMER0_CH1_P_POLARITY;
#endif
#if TIMER0_CH1_N_ENABLE
    tops.outputnstate = TIMER_CCXN_ENABLE;
    tops.ocnpolarity = TIMER0_CH1_N_POLARITY;
#endif
    // N和P
    tops.outputstate = (uint16_t)TIMER_CCX_ENABLE;
    timer_channel_output_config(timer, TIMER_CH_1, &tops);
    timer_channel_output_mode_config(timer, TIMER_CH_1, TIMER_OC_MODE_PWM0);
    timer_channel_output_pulse_value_config(timer, TIMER_CH_1, 0); // pwm值 初始duty =0
#endif
    //-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

    // -- -- -- -- -- -- -- -- - CH2 -- -- -- -- -- -- -- -- -- -- --
#if TIMER0_CH2_ENABLE
    timer_channel_output_struct_para_init(&tops);
#if TIMER0_CH2_P_ENABLE
    tops.outputstate = TIMER_CCX_ENABLE;
    tops.ocpolarity = TIMER0_CH2_P_POLARITY;
#endif
#if TIMER0_CH2_N_ENABLE
    tops.outputnstate = TIMER_CCXN_ENABLE;
    tops.ocnpolarity = TIMER0_CH2_N_POLARITY;
#endif
    // N和P
    tops.outputstate = (uint16_t)TIMER_CCX_ENABLE;
    timer_channel_output_config(timer, TIMER_CH_2, &tops);
    timer_channel_output_mode_config(timer, TIMER_CH_2, TIMER_OC_MODE_PWM0);
    timer_channel_output_pulse_value_config(timer, TIMER_CH_2, 0); // pwm值 初始duty =0
#endif
    //-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    // -- -- -- -- -- -- -- -- - CH3 -- -- -- -- -- -- -- -- -- -- --
#if TIMER0_CH3_ENABLE
    timer_channel_output_struct_para_init(&tops);
#if TIMER0_CH3_P_ENABLE
    tops.outputstate = TIMER_CCX_ENABLE;
    tops.ocpolarity = TIMER0_CH3_P_POLARITY;
#endif
    // N和P
    tops.outputstate = (uint16_t)TIMER_CCX_ENABLE;
    timer_channel_output_config(timer, TIMER_CH_3, &tops);
    timer_channel_output_mode_config(timer, TIMER_CH_3, TIMER_OC_MODE_PWM0);
    timer_channel_output_pulse_value_config(timer, TIMER_CH_3, 0); // pwm值 初始duty =0
#endif
    //-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

    /************************ 通道配置结束 ************************/

#if TIMER0_BRKIN_ENABLE

    /************************ 刹车功能配置 ************************/
    rcu_periph_clock_enable(TIMER0_BRKIN_PORT_RCU);
    gpio_mode_set(TIMER0_BRKIN_PORT, GPIO_MODE_INPUT, GPIO_PUPD_PULLDOWN, TIMER0_BRKIN_PIN);
    // 只针对高级定时器，break，保护电路，打开break电路
    timer_break_parameter_struct tbps;
    timer_break_struct_para_init(&tbps);
    //    tbps.breakpolarity = TIMER_BREAK_POLARITY_HIGH;//设置刹车输入信号的极性
    //    tbps.outputautostate = TIMER_OUTAUTO_ENABLE;//刹车输入引脚为高电平时，触发刹车功能。
    //    tbps.breakstate = TIMER_BREAK_ENABLE;//启用或禁用刹车功能
    //    tbps.deadtime = TIMER0_BRKIN_DEADTIME;


    tbps.runoffstate = TIMER_ROS_STATE_ENABLE;       // 运行模式下刹车使能
    tbps.ideloffstate = TIMER_IOS_STATE_ENABLE;       // 空闲模式下刹车使能
    tbps.deadtime = 0U;                           // 死区时间（根据需求调整）
    tbps.breakpolarity = TIMER_BREAK_POLARITY_HIGH;    // 高电平触发刹车
    tbps.outputautostate = TIMER_OUTAUTO_ENABLE;         // 自动输出状态使能
    tbps.protectmode = TIMER_CCHP_PROT_OFF;          // 保护模式关闭
    tbps.breakstate = TIMER_BREAK_ENABLE;           // 使能刹车功能
    timer_break_config(timer, &tbps);
    timer_break_enable(timer);

#endif
    timer_enable(timer); // 开启定时器
}

#if TIMER0_CH0_ENABLE
void timer0_ch0_update(float duty) {
    if (duty > 100)
        duty = 100;
    if (duty < 0)
        duty = 0;
    // uint32_t  timer_param_period = TIMER2_PERIOD; 设置频率
    uint32_t count = TIMER0_PERIOD + 1;
    uint32_t pulse = count * duty / 100 - 1;
    // 计数值
    timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_0, pulse); // pwm值
}
#endif
#if TIMER0_CH1_ENABLE
void timer0_ch1_update(float duty) {
    if (duty > 100)
        duty = 100;
    if (duty < 0)
        duty = 0;
    // uint32_t  timer_param_period = TIMER2_PERIOD; 设置频率
    uint32_t count = TIMER0_PERIOD + 1;
    uint32_t pulse = count * duty / 100 - 1;
    // 计数值
    timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_1, pulse); // pwm值
}
#endif
#if TIMER0_CH2_ENABLE
void timer0_ch2_update(float duty) {
    if (duty > 100)
        duty = 100;
    if (duty < 0)
        duty = 0;
    // uint32_t  timer_param_period = TIMER2_PERIOD; 设置频率
    uint32_t count = TIMER0_PERIOD + 1;
    uint32_t pulse = count * duty / 100 - 1;
    // 计数值
    timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_2, pulse); // pwm值
}
#endif
#if TIMER0_CH3_ENABLE
void timer0_ch3_update(float duty) {
    if (duty > 100)
        duty = 100;
    if (duty < 0)
        duty = 0;
    // uint32_t  timer_param_period = TIMER2_PERIOD; 设置频率
    uint32_t count = TIMER0_PERIOD + 1;
    uint32_t pulse = count * duty / 100 - 1;
    // 计数值
    timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_3, pulse); // pwm值
}
#endif

#endif